package com.qianjiangcloud.store.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import java.util.ArrayList;

import com.qianjiangcloud.store.R;


import com.qianjiangcloud.store.util.DisplayUtil;
import com.qianjiangcloud.store.util.NewsAdsUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 垂直滚动公告控件
 */
public class MarqueeView extends ViewFlipper {

    private Context mContext;
    private ArrayList<JSONObject> notices;
    private boolean isSetAnimDuration = false;
    private OnItemClickListener onItemClickListener;
    //滚动停留时间
    private int interval = 5000;
    //滚动切换动画时间
    private int animDuration = 500;
    private int textSize = 11;
    private int textColor = 0x474747;

    private boolean singleLine = false;
    private int gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
    private static final int TEXT_GRAVITY_LEFT = 0, TEXT_GRAVITY_CENTER = 1, TEXT_GRAVITY_RIGHT = 2;

    public MarqueeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.mContext = context;
        if (notices == null) {
            notices = new ArrayList();
        }

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MarqueeViewStyle, defStyleAttr, 0);
        interval = typedArray.getInteger(R.styleable.MarqueeViewStyle_mvInterval, interval);
        isSetAnimDuration = typedArray.hasValue(R.styleable.MarqueeViewStyle_mvAnimDuration);
        singleLine = typedArray.getBoolean(R.styleable.MarqueeViewStyle_mvSingleLine, false);
        animDuration = typedArray.getInteger(R.styleable.MarqueeViewStyle_mvAnimDuration, animDuration);
        if (typedArray.hasValue(R.styleable.MarqueeViewStyle_mvTextSize)) {
            textSize = (int) typedArray.getDimension(R.styleable.MarqueeViewStyle_mvTextSize, textSize);
            textSize = DisplayUtil.px2sp(mContext, textSize);
        }
        textColor = typedArray.getColor(R.styleable.MarqueeViewStyle_mvTextColor, textColor);
        int gravityType = typedArray.getInt(R.styleable.MarqueeViewStyle_mvGravity, TEXT_GRAVITY_RIGHT);
        switch (gravityType) {
            case TEXT_GRAVITY_CENTER:
                gravity = Gravity.CENTER;
                break;
            case TEXT_GRAVITY_RIGHT:
                gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
                break;
        }
        typedArray.recycle();

        setFlipInterval(interval);

        Animation animIn = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_in);
        if (isSetAnimDuration) animIn.setDuration(animDuration);
        setInAnimation(animIn);

        Animation animOut = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_out);
        if (isSetAnimDuration) animOut.setDuration(animDuration);
        setOutAnimation(animOut);
    }



    // 根据公告字符串列表启动轮播
    public void startWithList(ArrayList<JSONObject> notices) throws JSONException {
        setNotices(notices);
        start();
    }

    // 启动轮播
    public boolean start() throws JSONException {
        if (notices == null || notices.size() == 0) return false;
        removeAllViews();
        for (int i = 0; i < notices.size(); i+=2) {
            final LinearLayout mLinearLayout = creatLinearLayout(notices.get(i), i);
            final int finalI = i;
            mLinearLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(finalI, mLinearLayout);
                    }
                }
            });
            addView(mLinearLayout);
        }

        if (notices.size() > 2) {
            startFlipping();
        }else {
            stopFlipping();
        }

        return true;
    }
    //创建ViewFlipper下的LinearLayout
    private LinearLayout creatLinearLayout(JSONObject text, int position) throws JSONException {
        LinearLayout ll = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.ll_marquee, null);
        TextView tv_first = (TextView) ll.findViewById(R.id.tv_first);
        TextView tv_sec = (TextView) ll.findViewById(R.id.tv_sec);
        ImageView dot_sec = (ImageView) ll.findViewById(R.id.dot_sec);
        tv_first.setText(notices.get(position).getString("news_title"));
        tv_first.setTag(notices.get(position));
        tv_first.setOnClickListener(NewsAdsUtils.mNewsAdViewClickListener);

        if(position==notices.size()-1&&notices.size()!=1){
            tv_sec.setText(" ");
            dot_sec.setVisibility(GONE);
//            tv_sec.setText(notices.get(0).getString("news_title"));
//            tv_sec.setTag(notices.get(0));
//            tv_sec.setOnClickListener(NewsAdsUtils.mNewsAdViewClickListener);
        }else if(notices.size()==1){
            tv_sec.setText(" ");
            dot_sec.setVisibility(GONE);
        }else{
            tv_sec.setText(notices.get(position+1).getString("news_title"));
            tv_sec.setTag(notices.get(position+1));
            tv_sec.setOnClickListener(NewsAdsUtils.mNewsAdViewClickListener);
        }

        return ll;
    }


//    // 创建ViewFlipper下的TextView
//    private TextView createTextView(String text, int position) {
//        TextView tv = new TextView(mContext);
//        tv.setGravity(gravity);
//        tv.setText(text);
//        tv.setTextColor(textColor);
//        tv.setTextSize(textSize);
//        tv.setSingleLine(singleLine);
//        tv.setTag(position);
//        return tv;
//    }

//    public int getPosition() {
//        return (int) getCurrentView().getTag();
//    }

    public ArrayList<JSONObject> getNotices() {
        return notices;
    }

    public void setNotices(ArrayList<JSONObject> notices) {
        this.notices = notices;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, LinearLayout mLinearLayout);
    }

}
